define(['moment'], function (moment) {
    return ['$scope', '$rootScope', '$mdDialog', 'MMWService', '$filter', '$stateParams', '$timeout', 'config',
        function ($scope, $rootScope, $mdDialog, $MMWService, $filter, $stateParams, $timeout, config) {

            // 流程
            var levels = [
                [
                    'process_feeder_offline',
                    'process_reason',
                ],
                [
                    'process_feeder',
                    'process_material'
                    // 'process_slot_no' 20200708取消料站掃描功能
                ]
            ];

            $scope.init = function(){
                $scope.user_name = config.cache.name;
                $scope.envInfo = $filter('translate')('Login.env.'+config.setting.environment);
                // 預設流程狀態
                $scope.level_index = 0;
                $scope.phases_index = 0;
                $scope.phases = levels[$scope.level_index];
                $scope.isPrevPage = false;

                 // 自動隱蔽鍵盤
                 element_barcode_no = document.getElementById('input_barcode');
                 makeBarcodeInput(element_barcode_no);
                 element_barcode_no.focus();
            };

            $scope.back = function(){
                $scope.$root.hideMenu();
                $scope.$root.changeProgram('menu', true);
            };

            // QRCode輸入
            $scope.QRCodeScanner = function(){
                $scope.$root.OpenScanner({
                    after : function(result){
                        input_barcode_no_callback(result.text);
                    },
                    exception: function(){
                        //目前裝置無法使用相機
                        $scope.$root.showAlert($filter('translate')('common.msg.cant_use_camera'));
                    },
                    orientation : 'portrait'
                });
            };

            // barcode輸入
            $scope.input_barcode = function(barcode){
                input_barcode_no_callback(barcode);
            };

            // 流程狀態
            $scope.PhaseStatus = function(index){
                if($scope.phases_index > index)
                    return ['prev'];
                else if($scope.phases_index == index)
                    return ['now'];
                else
                    return ['next'];
            };

            // 回上一步流程
            $scope.BackToPhase = function (index){
                if($scope.phases_index > index){
                    $scope.phases_index = index;
                    if($scope.level_index == 0 && $scope.phases_index == 0){
                        // 清除 原feeder
                        $scope.machine_no = '';
                        $scope.origin_slot_no = '';
                        $scope.origin_reel_no = '';
                        $scope.origin_material_no = '';
                        $scope.origin_material_product_no = '';
                        $scope.origin_material_item_spec = '';
                        $scope.origin_material_qty = '';
                        $scope.origin_feeder_type_no = '';
                        $scope.origin_feeder_no = '';
                        $scope.origin_feeder_state = '';
                        $scope.mode = '';
                        $scope.mfu_no = '';
                        $scope.plot_no = '';
                        $scope.feederlist_no = '';
                        $scope.feederlist_ver = '';
                        $scope.actual_qty = '';
                        $scope.reason_no = '';
                        $scope.reason_name = '';
                        // 按確定後，如果回前頁，只有回到 Feeder下機流程，才能重新刷入feeder並重新輸入數量和異常原因
                        $scope.isPrevPage = false
                    } else if($scope.level_index == 1 && $scope.phases_index == 0){
                        //清除 新feeder
                        $scope.new_feeder_no = '';
                        $scope.new_feeder_state = '';
                        //清除 新物料
                        $scope.new_material_no = '';
                        $scope.new_material_product_no = '';
                        $scope.new_material_item_spec = '';
                        $scope.new_material_qty = '';
                        //清除 新料站
                        $scope.new_slot_no = '';
                    } else if($scope.level_index == 1 && $scope.phases_index == 1){
                        //清除 新物料
                        $scope.new_material_no = '';
                        $scope.new_material_product_no = '';
                        $scope.new_material_item_spec = '';
                        $scope.new_material_qty = '';
                        //清除 新料站
                        $scope.new_slot_no = '';
                    }
                };
            };

            // 切換頁面 ng-class
            $scope.LevelPhaseStatus = function(){
                return $scope.level_index == 0?'prevPage':'nextPage';
            };

            // 下一頁
            $scope.nextPage = function(){
                if($scope.level_index == 0 && $scope.phases_index == 1){
                    $scope.level_index = 1;
                    $scope.phases_index = 0;
                    $scope.phases = levels[$scope.level_index];
                    element_barcode_no.focus();
                };
            };

            // 上一頁
            $scope.prevPage = function(){
                // isPrevPage → 按確定後，回前頁時，數量 跟 更換原因 都不能在做修改！
                $scope.isPrevPage = true;
                if($scope.level_index == 1 && $scope.phases_index == 0){
                    $scope.level_index = 0;
                    $scope.phases_index = 1;
                    $scope.phases = levels[$scope.level_index];
                    element_barcode_no.focus();
                } else if($scope.level_index == 1 && $scope.phases_index == 3) {
                    $scope.level_index = 0;
                    $scope.phases_index = 0;
                    $scope.phases = levels[$scope.level_index];
                    $scope.machine_no = '';
                    $scope.origin_slot_no = '';
                    $scope.origin_material_no = '';
                    $scope.origin_material_product_no = '';
                    $scope.origin_material_item_spec = '';
                    $scope.origin_material_qty = '';
                    $scope.origin_feeder_no = '';
                    $scope.origin_feeder_state = '';
                    $scope.reason_no = '';
                    $scope.reason_name = '';
                    $scope.new_material_no = '';
                    $scope.new_material_product_no = '';
                    $scope.new_material_item_spec = '';
                    $scope.new_material_qty = '';
                    $scope.new_feeder_no = '';
                    $scope.new_feeder_state = '';
                    $scope.new_slot_no = '';
                } else{
                    $scope.$root.showConfirm($filter('translate')('SMT06.msg.prev-page_data-disappear'),
                        function(dialog){
                            dialog.hide();
                            $scope.level_index = 0;
                            $scope.phases_index = 1;
                            $scope.phases = levels[$scope.level_index];
                            //清除 新物料
                            $scope.new_material_no = '';
                            $scope.new_material_product_no = '';
                            $scope.new_material_item_spec = '';
                            $scope.new_material_qty = '';
                            //清除 新feeder
                            $scope.new_feeder_no = '';
                            $scope.new_feeder_state = '';
                            //清除 新料站
                            $scope.new_slot_no = '';
                    });
                };
            };
            
            function cleanAll(){
                $rootScope.showToast($filter('translate')('SMT06.msg.change_success'))
                $scope.level_index = 0;
                $scope.phases_index = 0;
                $scope.phases = levels[$scope.level_index];
                $scope.mode = '';
                $scope.machine_no ='';
                $scope.mfu_no ='';
                $scope.plot_no ='';
                $scope.feederlist_no ='';
                $scope.feederlist_ver ='';
                $scope.origin_slot_no ='';//null
                $scope.origin_material_no ='';
                $scope.origin_reel_no ='';
                $scope.origin_material_qty=''
                $scope.actual_qty ='';
                $scope.origin_material_qty ='';
                $scope.origin_feeder_no ='';
                $scope.origin_feeder_state ='';
                $scope.origin_material_product_no=''
                $scope.origin_material_item_spec ='';
                $scope.reason_no='';
                $scope.reason_name='';
                // 新物料新捲料
                $scope.origin_slot_no ='';
                $scope.new_feeder_no ='';
                $scope.new_feeder_state ='';
                $scope.new_material_no ='';
                $scope.new_reel_no ='';
                $scope.new_material_product_no ='';
                $scope.new_material_item_spec ='';
                $scope.new_material_qty ='';
            }

            // barcode輸入 callback
            function input_barcode_no_callback(barcode){
                if(barcode != ''){
                    // Feeder下機流程
                    if($scope.level_index == 0 && $scope.phases_index == 0){
                        feeder_info_get({
                            feeder_no: barcode
                        },function(data){
                            // 判斷 feeder是否在已上線狀態。 P.S.有綁定設備才算是已上線
                            if(data){
                                // 60 已上線
                                if(data.state == '60'){
                                    $scope.phases_index = 1;
                                    $scope.machine_no = data.equipment_no;
                                    $scope.origin_slot_no = data.slot_no;
                                    $scope.origin_reel_no = data.reel_no;
                                    $scope.origin_material_no = data.material_no;
                                    $scope.origin_material_product_no = data.product_no;
                                    $scope.origin_material_item_spec = data.item_spec;
                                    $scope.origin_material_qty = data.actual_qty;
                                    $scope.origin_feeder_type_no = data.feeder_type_no;
                                    $scope.origin_feeder_no = data.feeder_no;
                                    $scope.origin_feeder_state = data.state;
                                    $scope.mode = data.mode;
                                    $scope.mfu_no = data.mfu_no;
                                    $scope.plot_no = data.plot_no;
                                    $scope.feederlist_no = data.feederlist_no;
                                    $scope.feederlist_ver = data.feederlist_ver;
                                    $scope.actual_qty = data.actual_qty;
                                    $scope.reason_no = data.reason_no;
                                    $scope.reason_name = data.reason_name;
                                } else {
                                    $scope.$root.showAlert($filter('translate')('SMT06.msg.feeder_no_online'));
                                };
                            } else {
                                $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                            };
                        });
                    // 原因流程 (刷條碼變更)
                    } else if($scope.level_index == 0 && $scope.phases_index == 1){
                        // 按確定，並回前頁時，不能夠在刷入異常原因，請回到 Feeder下機流程
                        if($scope.isPrevPage != true){
                            tool_abnormal_reason_info_get(barcode, function(itemList){
                                if(itemList && itemList.length > 0){
                                    // is_default = 1 → 代表有設定異常原因的預設值
                                    for(var i = 0; i < itemList.length; i++){
                                        // 預帶有設定異常原因預設值的那筆資料
                                        if(itemList[i].is_default == 1){
                                            $scope.reason_no = itemList[i].reason_no;
                                            $scope.reason_name = itemList[i].reason_name;
                                        } else {
                                            $scope.$root.showAlert($filter('translate')('SMT06.msg.not_set_default_abnormal_reason'));
                                        };
                                    };
                                } else {
                                    $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                                };
                            });
                        } else {
                            $scope.$root.showAlert($filter('translate')('SMT06.msg.prev_page_canNot_modify_abnormal_reason'));
                        };
                    // 新 Feeder 流程
                    } else if($scope.level_index == 1 && $scope.phases_index == 0){
                        feeder_info_get_page_mode(barcode, function(itemList){
                            // 比照上料比對功能，在庫狀態和已上料狀態才能成功，其他狀態提示異常
                            if(itemList && itemList.length){
                                // 0 在庫
                                if(itemList[0].state == '0'){
                                    var content = {
                                        feeder_no : itemList[0].feeder_no
                                    };
                                    // 在庫狀態才做檢查
                                    feeder_check_process(content,function(){
                                        $scope.phases_index = 1;
                                        $scope.new_feeder_type_no = itemList[0].feeder_type_no;
                                        $scope.new_feeder_no = itemList[0].feeder_no;
                                        $scope.new_feeder_state = itemList[0].state;
                                    });
                                // 40 已上料 → 如果 feeder 是已上料狀態，會同時刷入 feeder 和 捲料(物料)資料
                                } else if(itemList[0].state == '40'){
                                    $scope.phases_index = 2;
                                    $scope.new_feeder_type_no = itemList[0].feeder_type_no;
                                    $scope.new_feeder_no = itemList[0].feeder_no;
                                    $scope.new_feeder_state = itemList[0].state;
                                    $scope.new_reel_no = itemList[0].reel_no;
                                    $scope.new_material_no = itemList[0].material_no;
                                    $scope.new_material_product_no = itemList[0].product_no;
                                    $scope.new_material_item_spec = itemList[0].item_spec;
                                    $scope.new_material_qty = itemList[0].qty;

                                    let content = {
                                        // feeder_type_no: $scope.feeder_type_no,//null
                                        // feeder_no: $scope.feeder_no,//null
                                        
                                        feeder_mode: $scope.mode,
                                        equipment_no: $scope.machine_no,
                                        mfu_no: $scope.mfu_no,
                                        plot_no: $scope.plot_no,
                                        feederlist_no: $scope.feederlist_no,
                                        feederlist_ver: $scope.feederlist_ver,
                                        slot_no: $scope.origin_slot_no,//null
                                        material_no: $scope.origin_material_no,
                                        reel_no: $scope.origin_reel_no,
                                        actual_qty: $scope.actual_qty,
                                        material_qty:$scope.origin_material_qty,
                                        //舊Feeder
                                        feeder_type_no:"FD",
                                        feeder_no:$scope.origin_feeder_no,
                                        feeder_state :$scope.origin_feeder_state,
                                        // 新物料新捲料
                                        new_feeder_type_no:"FD",
                                        new_slot_no: $scope.origin_slot_no,
                                        new_feeder_no:$scope.new_feeder_no,
                                        new_feeder_state:$scope.new_feeder_state,
                                        new_material_no: $scope.new_material_no,
                                        new_reel_no: $scope.new_reel_no,
                                        new_product_no:$scope.new_material_product_no,
                                        new_item_spec:$scope.new_material_item_spec,
                                        new_qty:$scope.new_material_qty
                                        // qty: $scope.new_material_actual_qty//null
                                    };
                                    feeder_reel_change_set_process(content,function(){
                                        cleanAll();
                                    });
                                // 80 報廢
                                } else if(itemList[0].state == '80'){
                                    $scope.$root.showAlert($filter('translate')('SMT.common.msg.feeder_scrapped'));
                                // 70 送修
                                } else if(itemList[0].state == '70'){
                                    $scope.$root.showAlert($filter('translate')('SMT.common.msg.feeder_repair'));
                                // 50 已上台車
                                } else if (itemList[0].state == '50'){
                                    $scope.$root.showAlert($filter('translate')('SMT.common.msg.feeder_already_feeding'));
                                // 60 已上線
                                } else if (itemList[0].state == '60'){
                                    $scope.$root.showAlert($filter('translate')('SMT.common.msg.feeder_already_online'));
                                // 不存在
                                } else {
                                    $scope.$root.showAlert($filter('translate')('SMT.common.msg.no_feeder'));
                                };
                            } else {
                                $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                            };
                        });
                    // 物料流程
                    } else if($scope.level_index == 1 && $scope.phases_index == 1) {
                        material_info_get(barcode, function(itemList){
                            // 在庫狀態才能成功，其他狀態提示異常
                            if(itemList && itemList.length > 0){
                                if((+itemList.remain_time) <= 0 && itemList.remain_time!='-1'){
                                    $scope.$root.showAlert($filter('translate')('SMT06.msg.MSD_over_time'));
                                }else{
                                    // 在庫 0
                                    if(itemList[0].state == '0'){
                                        // $scope.phases_index = 2;
                                        $scope.new_reel_no = itemList[0].reel_no;
                                        $scope.new_material_no = itemList[0].material_no;
                                        $scope.new_material_product_no = itemList[0].product_no;
                                        $scope.new_material_item_spec = itemList[0].item_spec;
                                        $scope.new_material_qty = itemList[0].qty;

                                        let content = {
                                            // feeder_type_no: $scope.feeder_type_no,//null
                                            // feeder_no: $scope.feeder_no,//null
                                            
                                            feeder_mode: $scope.mode,
                                            equipment_no: $scope.machine_no,
                                            mfu_no: $scope.mfu_no,
                                            plot_no: $scope.plot_no,
                                            feederlist_no: $scope.feederlist_no,
                                            feederlist_ver: $scope.feederlist_ver,
                                            slot_no: $scope.origin_slot_no,//null
                                            material_no: $scope.origin_material_no,
                                            reel_no: $scope.origin_reel_no,
                                            actual_qty: $scope.actual_qty,
                                            material_qty:$scope.origin_material_qty,
                                            //舊Feeder
                                            feeder_type_no:"FD",
                                            feeder_no:$scope.origin_feeder_no,
                                            feeder_state :$scope.origin_feeder_state,
                                            // 新物料新捲料
                                            new_feeder_type_no:"FD",
                                            new_feeder_no:$scope.new_feeder_no,
                                            new_slot_no: $scope.origin_slot_no,
                                            new_feeder_state:$scope.new_feeder_state,
                                            new_material_no: $scope.new_material_no,
                                            new_reel_no: $scope.new_reel_no,
                                            new_product_no:$scope.new_material_product_no,
                                            new_item_spec:$scope.new_material_item_spec,
                                            new_qty:$scope.new_material_qty
                                            // qty: $scope.new_material_actual_qty//null
                                        };
                                        feeder_reel_change_set_process(content,function(){
                                            cleanAll();
                                        });

                                    // 80 報廢
                                    } else if(itemList[0].state == '80'){
                                        $scope.$root.showAlert($filter('translate')('SMT.common.msg.reel_scrapped'));
                                    // 40 已上料
                                    } else if (itemList[0].state == '40'){
                                        $scope.$root.showAlert($filter('translate')('SMT.common.msg.reel_already_feeding'));
                                    // 60 已上線
                                    } else if (itemList[0].state == '60'){
                                        $scope.$root.showAlert($filter('translate')('SMT.common.msg.reel_already_online'));
                                    // 不存在
                                    } else {
                                        $scope.$root.showAlert($filter('translate')('SMT.common.msg.no_reel'));
                                    };
                                }
                            } else {
                                $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                            };
                        });
                    } else {
                        $scope.$root.showAlert($filter('translate')('SMT.common.msg.process_error'));
                    };
                };
            };

            // 更換原因開窗
            $scope.loadReason = function () {
                $MMWService.showQueryListPNIG({
    				page_mode: 'tool_abnormal_reason_info_get',
                    key: 'reason_no',
                    header: [
                        {label:$filter('translate')('SMT06.list.change_feeder_reason'), code:'reason_no'},
                        {label:$filter('translate')('SMT06.list.change_feeder_remark'), code:'reason_name'}
                    ],
                    setCondition:function(conditions){
                        conditions.push({column_name:'ReasonType', query_mode:'0', value:'0', merge_condition_model:'AND' });
                        return conditions;
                    },
                    confirm: function(selectproduct, dialog){
                        dialog.hide();
                        $scope.reason_no = selectproduct.reason_no;
                        $scope.reason_name = selectproduct.reason_name;
                    }
                });
            };

            // 帶出 Feeder資訊
            function feeder_info_get(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.feeder_info_get',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            };

            // 取得 Feeder資料 (page_mode)
            function feeder_info_get_page_mode(feeder_no, callback){
                $MMWService.pageNumberInfoGet({
                    page_mode : 'feeder_info_get',
                    query_condition_info:[
                        {column_name:'FeederNo', query_mode:'0', value: feeder_no, merge_condition_model:'AND'},
                    ]
                }, function(itemList){
                    if(callback)
                        callback(itemList);
                });
            };

            // 取得 工具異常原因
            function tool_abnormal_reason_info_get(reason_no, callback){
                $MMWService.pageNumberInfoGet({
                    page_mode : 'tool_abnormal_reason_info_get',
                    query_condition_info:[
                        {column_name:'ReasonType', query_mode:'0', value: reason_no, merge_condition_model:'AND'},
                    ]
                }, function(itemList){
                    if(callback)
                        callback(itemList);
                });
            };

            // Feeder檢查
            function feeder_check_process(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.feeder_mfu_check_process',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            };

            // 取得 捲料資料
            function material_info_get(reel_no, callback){
                let content = {
                    reel_no:reel_no,
                }
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.reel_info_get',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            };

            // 料站資訊取得
            function slot_info_get(slot_no, callback){
                $MMWService.pageNumberInfoGet({
                    page_mode : 'slot_info_get',
                    query_condition_info:[
                        {column_name:'EquipmentNo', query_mode:'0', value: $scope.machine_no, merge_condition_model:'AND'},
                        {column_name:'SlotNo', query_mode:'0', value: slot_no, merge_condition_model:'AND'},
                    ]
                }, function(itemList){
                    if(callback)
                        callback(itemList);
                });
            };

            function feeder_reel_change_set_process(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.feeder_change_set_process',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            };

            $scope.input_PDA_barcode = function(event){
                if (event.keyCode == 13) {
                    element_barcode_no.blur();
                    input_barcode_no_callback($scope.barcode)
                    $scope.barcode = '';
                } else {
                    $scope.barcode = $scope.barcode || '';
                }
            }

            //20200914 modify by Dustdusk for M#: 增加
            function makeBarcodeInput(el) {
                var _input = el.getElementsByClassName('barcode_input')[0];
                //var _mask = el.getElementsByClassName('barcode_mask')[0];
                el.focus = function () {
                    //e.preventDefault();
                    //_mask.click();
                    el.click();
                }
                el.addEventListener('click', function (e) {
                    e.preventDefault();
                    //_input.value = '';
                    if (document.activeElement !== _input)
                        _input.focus();
                });
                _input.addEventListener('focus', function (e) {
                    e.preventDefault();
                    setTimeout(function () {
                        _input.removeAttribute('readonly');
                    }, 200);
                });
                _input.addEventListener('blur', function (e) {
                    e.preventDefault();
                    _input.setAttribute('readonly', 'readonly');
                });
            }
        }
    ];
});
